package cn.tedu.system.user.controller;


import cn.tedu.system.base.response.JsonResult;
import cn.tedu.system.user.pojo.dto.UserLoginDto;
import cn.tedu.system.user.pojo.dto.UserRegDto;
import cn.tedu.system.user.pojo.dto.UserReviseDto;
import cn.tedu.system.user.pojo.vo.UserVO;
import cn.tedu.system.user.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Slf4j
@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/api/v1/users/")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("注册功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("public/reg")
    public JsonResult register(@RequestBody UserRegDto userRegDto) {
        log.info("用户注册参数:{}", userRegDto);
        userService.registerUser(userRegDto);
        return JsonResult.ok();
    }

    @ApiOperation("登录功能")
    @ApiOperationSupport(order = 20)
    @PostMapping("public/login")
    public JsonResult login(@RequestBody UserLoginDto userLoginDto, @ApiIgnore HttpSession session) {
        log.debug("用户登录参数:{}", userLoginDto);
        UserVO userVO = userService.loginUser(userLoginDto,session);
        return JsonResult.ok(userVO);
    }

    @ApiOperation("个人信息修改功能")
    @ApiOperationSupport(order = 30)
    @PostMapping("private/revise")
    public JsonResult revise(@RequestBody UserReviseDto userReviseDto, @ApiIgnore HttpSession session) {
        log.debug("用户修改参数:{}", userReviseDto);
        userService.reviseUser(userReviseDto,session);
        return JsonResult.ok();
    }

    @ApiOperation("退出登录功能")
    @ApiOperationSupport(order = 40)
    @PostMapping("public/logout")
    public JsonResult logout(@ApiIgnore HttpSession session) {
        userService.logoutUser(session);
        return JsonResult.ok();
    }

    @ApiOperation("查询个人信息")
    @ApiOperationSupport(order = 50)
    @GetMapping("select")
    public JsonResult select(@ApiIgnore HttpSession session) {
        UserVO userVO = userService.selectUser(session);
        return JsonResult.ok(userVO);
    }
}
